﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.IO;

namespace SimpleSMSProvider
{
    class WebServiceConnector
    {
        private IPEndPoint ipEnd;
        public int IsConnected = 0;
        private Socket m_clientSocket;

        public WebServiceConnector(IPAddress ip, int Port)
        {
            this.m_clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            this.ipEnd = new IPEndPoint(ip, Port);
            this.IsConnected = 0;
        }

        public void SendMessage(string Msg)
        {
            if ((this.m_clientSocket != null) && this.m_clientSocket.Connected)
            {
                try
                {
                    NetworkStream stream = new NetworkStream(this.m_clientSocket);
                    StreamWriter writer = new StreamWriter(stream);
                    object obj2 = Msg;
                    object[] objArray = new object[] { obj2, Convert.ToChar(1), (Msg.Length + 1).ToString(), Convert.ToChar(2) };
                    Msg = string.Concat(objArray);
                    writer.Write(Msg);
                    writer.Flush();
                }
                catch
                {
                    this.Disconnect(true);
                }
            }
        }

        public void Disconnect(bool p)
        {
            if (this.m_clientSocket != null)
            {
                this.m_clientSocket.Close();
            }
        }
    }

    
}
